package com.ruoyi.msg.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.collection.CollUtil;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.msg.domain.MsgDcwjWjTk;
import com.ruoyi.msg.domain.bo.MsgDcwjWjBo;
import com.ruoyi.msg.domain.bo.WjXtBatchBo;
import com.ruoyi.msg.domain.vo.MsgDcwjTkVo;
import com.ruoyi.msg.domain.vo.MsgDcwjWjVo;
import com.ruoyi.msg.domain.vo.TkXxDetailVo;
import com.ruoyi.msg.service.IMsgDcwjWjService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 问卷管理
 *
 * @author xiaohao
 * @date 2023-02-02
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/msg/dcwjWj")
public class MsgDcwjWjController extends BaseController {

    private final IMsgDcwjWjService iMsgDcwjWjService;

    /**
     * 题库列表【已选】（当前问卷已选的题库信息列表）
     *
     * @param id 问卷id
     */
    @GetMapping("/tk/selectedList")
    public R<List<TkXxDetailVo>> selectedList(@RequestParam Long id,
                                              @RequestParam(required = false) String name,
                                              @RequestParam(required = false) String type) {
        return R.ok(iMsgDcwjWjService.selectedList(id, name, type));
    }

    /**
     * 题库列表【待选】（未被当前问卷选择过的）
     */
    @GetMapping("/tk/chooseList")
    public TableDataInfo<MsgDcwjTkVo> chooseList(@RequestParam Long id,
                                                 PageQuery pageQuery) {
        return iMsgDcwjWjService.chooseList(id, pageQuery);
    }

    /**
     * 问卷选题（批量添加）
     */
    @PostMapping("/tk/saveBatch")
    public R<String> saveBatch(@RequestBody WjXtBatchBo bo) {
        List<MsgDcwjWjTk> list = bo.getList();
        if (CollUtil.isNotEmpty(list)) {
            iMsgDcwjWjService.saveBatch(bo);
            return R.ok("操作成功");
        } else {
            return R.fail("未选择对应题库内容");
        }
    }

    /**
     * 查询问卷管理列表
     */
    @SaCheckPermission("msg:dcwjWj:list")
    @GetMapping("/list")
    public TableDataInfo<MsgDcwjWjVo> list(MsgDcwjWjBo bo, PageQuery pageQuery) {
        return iMsgDcwjWjService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出问卷管理列表
     */
    @SaCheckPermission("msg:dcwjWj:export")
    @Log(title = "问卷管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(MsgDcwjWjBo bo, HttpServletResponse response) {
        List<MsgDcwjWjVo> list = iMsgDcwjWjService.queryList(bo);
        ExcelUtil.exportExcel(list, "问卷管理", MsgDcwjWjVo.class, response);
    }

    /**
     * 获取问卷管理详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("msg:dcwjWj:query")
    @GetMapping("/{id}")
    public R<MsgDcwjWjVo> getInfo(@NotNull(message = "主键不能为空")
                                  @PathVariable Long id) {
        return R.ok(iMsgDcwjWjService.queryById(id));
    }

    /**
     * 新增问卷管理
     */
    @SaCheckPermission("msg:dcwjWj:add")
    @Log(title = "问卷管理", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody MsgDcwjWjBo bo) {
        return toAjax(iMsgDcwjWjService.insertByBo(bo));
    }

    /**
     * 修改问卷管理
     */
    @SaCheckPermission("msg:dcwjWj:edit")
    @Log(title = "问卷管理", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody MsgDcwjWjBo bo) {
        return toAjax(iMsgDcwjWjService.updateByBo(bo));
    }

    /**
     * 删除问卷管理
     *
     * @param ids 主键串
     */
    @SaCheckPermission("msg:dcwjWj:remove")
    @Log(title = "问卷管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iMsgDcwjWjService.deleteWithValidByIds(Arrays.asList(ids), true));
    }
}
